package com.whz.governance.sensitive.utils;

import com.whz.generic.spring.SpringUtils;
import com.whz.governance.sensitive.SensitiveEnum;
import com.whz.governance.sensitive.SensitiveStoreUploader;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.util.Set;

/**
 * @author whz
 */
@Slf4j
@UtilityClass
public class SensitiveUploadUtils {

    /**
     * 增量更新敏感数据
     *
     * @param sensitiveEnum 敏感数据类型
     * @param data          敏感数据
     */
    public void uploadContinue(SensitiveEnum sensitiveEnum, Set<String> data) {
        SpringUtils.getBean(SensitiveStoreUploader.class).ifPresent(sensitiveStoreUploader -> {
            try {
                boolean result = sensitiveStoreUploader.uploadContinue(sensitiveEnum, data);
                log.info("uploadContinue '{}' result:{}", sensitiveEnum, result);
            } catch (Exception exception) {
                log.error("uploadContinue '" + sensitiveEnum + "' error.", exception);
            }
        });
    }

    /**
     * 覆盖更新敏感数据
     *
     * @param sensitiveEnum 敏感数据类型
     * @param data          敏感数据
     */
    public void uploadOverride(SensitiveEnum sensitiveEnum, Set<String> data) {
        SpringUtils.getBean(SensitiveStoreUploader.class).ifPresent(sensitiveStoreUploader -> {
            try {
                boolean result = sensitiveStoreUploader.uploadOverride(sensitiveEnum, data);
                log.info("uploadOverride '{}' result:{}", sensitiveEnum, result);
            } catch (Exception exception) {
                log.error("uploadOverride '" + sensitiveEnum + "' error.", exception);
            }
        });
    }

    /**
     * 增量上传ip黑名单
     *
     * @param ips 黑名单
     */
    public void uploadContinueForIpBlackList(Set<String> ips) {
        uploadContinue(SensitiveEnum.IP_BLACK, ips);
    }

    /**
     * 增量上传IP白名单
     *
     * @param ips ip白名单
     */
    public void uploadContinueForIpWhiteList(Set<String> ips) {
        uploadContinue(SensitiveEnum.IP_WHITE, ips);
    }

    /**
     * 覆盖上传ip白名单
     *
     * @param ips 白名单
     */
    public void uploadOverrideForIpWhiteList(Set<String> ips) {
        uploadOverride(SensitiveEnum.IP_WHITE, ips);
    }

    /**
     * 覆盖上传ip黑名单
     *
     * @param ips 黑名单
     */
    public void uploadOverrideForIpBlackList(Set<String> ips) {
        uploadOverride(SensitiveEnum.IP_BLACK, ips);
    }
}